package cn.youwan.market.utils;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import android.content.Context;
import android.os.Environment;
import android.os.StatFs;
import android.util.Log;

public class FileUtils
{
	
	/**
	 * 写文本文件
	 * 在Android系统中，文件保存在 /data/data/PACKAGE_NAME/files 目录下
	 * @param context
	 * @param msg
	 */
	public static void write(Context context, String fileName, String content)
	{
		if (content == null)
			content = "";
		
		try
		{
			FileOutputStream fos = context.openFileOutput(fileName, Context.MODE_PRIVATE);
			fos.write(content.getBytes());
			
			fos.close();
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}
	
	/**
	 * 读取文本文件
	 * @param context
	 * @param fileName
	 * @return
	 */
	public static String read(Context context, String fileName)
	{
		try
		{
			FileInputStream in = context.openFileInput(fileName);
			return readInStream(in);
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		return "";
	}
	
	private static String readInStream(FileInputStream inStream)
	{
		try
		{
			ByteArrayOutputStream outStream = new ByteArrayOutputStream();
			byte[] buffer = new byte[512];
			int length = -1;
			while ((length = inStream.read(buffer)) != -1)
			{
				outStream.write(buffer, 0, length);
			}
			
			outStream.close();
			inStream.close();
			return outStream.toString();
		}
		catch (IOException e)
		{
			Log.i("FileTest", e.getMessage());
		}
		return null;
	}
	
	/**
	 * 向手机写图片
	 * @param buffer   
	 * @param folder
	 * @param fileName
	 * @return
	 */
	public static boolean writeFile(byte[] buffer, String folder, String fileName)
	{
		boolean writeSucc = false;
		
		boolean sdCardExist = Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
		
		String folderPath = "";
		if (sdCardExist)
		{
			folderPath = Environment.getExternalStorageDirectory() + File.separator + folder + File.separator;
		}
		else
		{
			writeSucc = false;
		}
		
		File fileDir = new File(folderPath);
		if (!fileDir.exists())
		{
			fileDir.mkdirs();
		}
		
		File file = new File(folderPath + fileName);
		FileOutputStream out = null;
		try
		{
			out = new FileOutputStream(file);
			out.write(buffer);
			writeSucc = true;
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			try
			{
				out.close();
			}
			catch (IOException e)
			{
				e.printStackTrace();
			}
		}
		
		return writeSucc;
	}
	
	/**
	 * 根据文件绝对路径获取文件名
	 * @param filePath
	 * @return
	 */
	public static String getFileName(String filePath)
	{
		if (StringUtils.isEmpty(filePath))
			return "";
		return filePath.substring(filePath.lastIndexOf(File.separator) + 1);
	}
	
	/**
	 * 根据文件的绝对路径获取文件名但不包含扩展名
	 * @param filePath
	 * @return
	 */
	public static String getFileNameNoFormat(String filePath)
	{
		if (StringUtils.isEmpty(filePath))
		{
			return "";
		}
		int point = filePath.lastIndexOf('.');
		return filePath.substring(filePath.lastIndexOf(File.separator) + 1, point);
	}
	
	/**
	 * 获取文件大小
	 * @param size 字节
	 * @return
	 */
	public static String getFileSize(long size)
	{
		if (size <= 0)
			return "0";
		java.text.DecimalFormat df = new java.text.DecimalFormat("##.##");
		
		/*float temp = (float) size / 1024;
		if (temp >= 1024)
		{
			return df.format(temp / 1024) + "M";
		}
		else
		{
			return df.format(temp) + "K";
		}*/
		
		return df.format( (float) size / 1024 / 1024) + "M";
	}
	
	public static String getFileSize2(long size)
	{
		if (size <= 0)
			return "0";
		
		java.text.DecimalFormat df = new java.text.DecimalFormat();
		
		float temp = (float) size / 1024;
		
		if (temp >= 1024)
		{
			df.applyPattern("##.##");
			return df.format(temp / 1024) + "M";
		}
		else
		{
			df.applyPattern("####");
			return df.format(temp) + "K";
		}
	}
	
	
	public static File createFile(String folderPath, String fileName)
	{
		File destDir = new File(folderPath);
		if (!destDir.exists())
		{
			destDir.mkdirs();
		}
		return new File(folderPath, fileName);
	}
	
	/**
	 * @return SDcard上剩余的存储空间（以M为单位）
	 */
	public long getAvailaleSize()
	{
		
		File path = Environment.getExternalStorageDirectory(); //取得sdcard文件路径
		StatFs stat = new StatFs(path.getPath());
		
		long blockSize = stat.getBlockSize();
		long availableBlocks = stat.getAvailableBlocks();
		
		//return availableBlocks * blockSize;	  //Byte单位
		//(availableBlocks * blockSize)/1024;      //KIB 单位
		return (availableBlocks * blockSize)/1024 /1024;  //MIB单位
	}
	
	public long getAllSize()
	{
		
		File path = Environment.getExternalStorageDirectory();
		
		StatFs stat = new StatFs(path.getPath());
		
		long blockSize = stat.getBlockSize();
		
		long availableBlocks = stat.getBlockCount();
		
		return availableBlocks * blockSize;
		
	}
	
	/**
	 * 计算SD卡的剩余空间 (Mib单位)
	 * @return 返回-1，说明没有安装sd卡
	 */
	public static long getFreeDiskSpace()
	{
		String status = Environment.getExternalStorageState();
		long freeSpace = 0;
		if (status.equals(Environment.MEDIA_MOUNTED))
		{
			try
			{
				File path = Environment.getExternalStorageDirectory();
				StatFs stat = new StatFs(path.getPath());
				long blockSize = stat.getBlockSize();
				long availableBlocks = stat.getAvailableBlocks();
				freeSpace = availableBlocks * blockSize / 1024 /1024;
			}
			catch (Exception e)
			{
				e.printStackTrace();
			}
		}
		else
		{
			return -1;
		}
		return (freeSpace);
	}
	
	/**
	 * 检查是否安装SD卡
	 * @return
	 */
	public static boolean checkSaveLocationExists()
	{
		String sDCardStatus = Environment.getExternalStorageState();
		boolean status;
		if (sDCardStatus.equals(Environment.MEDIA_MOUNTED))
		{
			status = true;
		}
		else
			status = false;
		return status;
	}
	
	/**
	 * 删除目录(包括：目录里的所有文件)
	 * @param fileName
	 * @return
	 */
	public static boolean deleteDirectory(String fileName)
	{
		boolean status;
		SecurityManager checker = new SecurityManager();
		
		if (!fileName.equals(""))
		{
			
			//File path = Environment.getExternalStorageDirectory();
			//File newPath = new File(path.toString() + fileName);
			
			File newPath = new File(fileName);
			checker.checkDelete(newPath.toString());
			if (newPath.isDirectory())
			{
				String[] listfile = newPath.list();
				// delete all files within the specified directory and then
				// delete the directory
				try
				{
					for (int i = 0; i < listfile.length; i++)
					{
						File deletedFile = new File(newPath.toString() + "/" + listfile[i].toString());
						deletedFile.delete();
					}
					newPath.delete();
					Log.i("DirectoryManager deleteDirectory", fileName);
					status = true;
				}
				catch (Exception e)
				{
					e.printStackTrace();
					status = false;
				}
				
			}
			else
				status = false;
		}
		else
			status = false;
		return status;
	}
	
	/**
	 * 删除文件
	 * @param fileName
	 * @return
	 */
	public static boolean deleteFile(String fileName)
	{
		boolean status;
		SecurityManager checker = new SecurityManager();
		
		if (!fileName.equals(""))
		{
			
			File path = Environment.getExternalStorageDirectory();
			File newPath = new File(path.toString() + fileName);
			checker.checkDelete(newPath.toString());
			if (newPath.isFile())
			{
				try
				{
					Log.i("DirectoryManager deleteFile", fileName);
					newPath.delete();
					status = true;
				}
				catch (SecurityException se)
				{
					se.printStackTrace();
					status = false;
				}
			}
			else
				status = false;
		}
		else
			status = false;
		return status;
	}
	
	/**
	 * 删除下载的apk文件
	 * @param fileName 
	 */
	public static boolean deleteApkFile(String fileName)
	{
		boolean res = false;
		//文件如果是未下载完的，则无扩展名；下载完后已.apk为扩展名
		if(fileName.endsWith(".apk"))
		{
			File f = new File(fileName);
			if(f.exists())
			{
				res = f.delete();;
			}
			else
			{
				int point = fileName.lastIndexOf('.');
				f = new File(fileName.substring(0, point));
				if(f.exists())
				{	
					res = f.delete();
				}
				
			}
		}
		else
		{
			File f = new File(fileName);
			if(f.exists())
			{
				res = f.delete();
			}
			else
			{
				f = new File(fileName+".apk");
				if(f.exists())
				{
					res = f.delete();
				}
			}
		}
		return res;
	}
}
